<?php

namespace app\controller\admin;

use think\facade\Request;
use app\common\model\Admin;
use app\common\util\Result;
use think\annotation\route\Get;
use think\annotation\route\Post;
use think\annotation\route\Group;

#[Group('admin/me')]
class MeController extends AdminAuthorizedController {

    #[Post('password/reset')]
    public function resetPassword(Admin $admin) {
        ['password' => $password, 'newPassword' => $newPassword] = $param = Request::post(['password', 'newPassword']);
        $this->validate($param, [
            'password' => 'require|max:50',
            'newPassword' => 'require|max:50',
        ]);
        if (!password_verify($password, $admin->password)) {
            return Result::restf(231, '密码错误');
        }
        $admin->password = password_hash($newPassword, PASSWORD_BCRYPT);
        $admin->save();
        return Result::rest();
    }

    #[Post('update')]
    public function update(Admin $admin) {
        $param = Request::post(['real_name', 'phone']);
        return Result::rest($admin->save($param));
    }

    #[Get('whoami')]
    public function whoami(Admin $admin) {
        return Result::rest($admin->hidden(['password'])->append(["admin_role"]));
    }
}
